﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GoShopping.Model.Enumerations;
using GoShopping.Model;
using GoShopping.BLL;

namespace GoShopping.Web.SuperManager
{
    public partial class EmailTemplet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { //enableSessionState="true" validateRequest="false"
                bindEmailType();
                if (Request.QueryString["Type"] == "Update")
                {
                    bindEmailTempl();
                    ddlEmailType.SelectedValue = Request.QueryString["Name"].ToString();
                }
            }
        }

        /// <summary>
        /// 绑定邮件类型
        /// </summary>
        void bindEmailType()
        {
            IList<Utility.EnumBoundings.ComboBoxItem> cbiList = Utility.EnumBoundings.OptionEnum.GetEnumNameValueKeys(Model.Enumerations.EmailTempleteType.ChangePassWord.GetType());
            ddlEmailType.DataTextField = "Name";
            ddlEmailType.DataValueField = "Value";
            ddlEmailType.DataSource = cbiList;
            ddlEmailType.DataBind();

            #region temp
            ////DataTable dt = new DataTable();
            ////dt.Columns.Add("TypeName");
            ////dt.Columns.Add("TypeValue");
            ////dt.Rows.Add(new object[] { "会员注册", EmailTempleteType.UserRegist.ToString() });
            ////dt.Rows.Add(new object[] { "更改密码", EmailTempleteType.ChangePassWord.ToString() });
            ////dt.Rows.Add(new object[] { "找回密码", EmailTempleteType.FindPassword.ToString() });
            ////dt.Rows.Add(new object[] { "订单创建", EmailTempleteType.OrderCreated.ToString() });
            ////dt.Rows.Add(new object[] { "订单发货", EmailTempleteType.OrderPost.ToString() });
            ////dt.Rows.Add(new object[] { "订单作废", EmailTempleteType.OrderCancle.ToString() });
            #endregion
        }

        /// <summary>
        /// 绑定邮件模板
        /// </summary>
        void bindEmailTempl()
        {
            EmailTempleteBean etbean = new EmailTempleteBean();
            EmailTemplete etp = null; ;
            switch(Request.QueryString["Name"])
            {
                case "ChangePassWord":
                    etp= etbean.GetEmailTemplete(EmailTempleteType.ChangePassWord);
                    break;
                case "FindPassword":
                    etp = etbean.GetEmailTemplete(EmailTempleteType.FindPassword);
                    break;
                case "OrderCancle":
                    etp = etbean.GetEmailTemplete(EmailTempleteType.OrderCancle);
                    break;
                case "OrderCreated":
                    etp = etbean.GetEmailTemplete(EmailTempleteType.OrderCreated);
                    break;
                case "OrderPost":
                    etp = etbean.GetEmailTemplete(EmailTempleteType.OrderPost);
                    break;
                case "UserRegist":
                    etp = etbean.GetEmailTemplete(EmailTempleteType.UserRegist);
                    break;
            }
            txtDesc.Text = etp.ConfigExplain;
            txtTile.Text = etp.EmailTitle;
            CBIsUs.Checked = (bool)etp.IsInUse;
            FreeTextBox1.Text = etp.ContentHTML;
           
        }

        /// <summary>
        /// 更新邮件模板的按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {            
            EmailTemplete EmTempl = new EmailTemplete();
            EmTempl.EmailTitle = txtTile.Text.ToString();   //邮件标题
            EmTempl.IsInUse = CBIsUs.Checked;               //是否候选中
            EmTempl.ConfigExplain = txtDesc.Text.ToString(); //邮件配置说明
            EmTempl.Name = ddlEmailType.SelectedItem.Value.ToString();  //邮件模板名称
            EmTempl.ContentHTML = FreeTextBox1.Text.ToString();
            bool EmUpdate = false;
            EmailTempleteBean EmTemplBean = new EmailTempleteBean();
            switch (ddlEmailType.SelectedValue)
            {
                case "0":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.UserRegist);
                    break;
                case "1":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.ChangePassWord);
                    break;
                case "2":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.FindPassword);
                    break;
                case "3":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderCreated);
                    break;
                case "4":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderPost);
                    break;
                case "5":
                    EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderCancle);
                    break;
            }
            #region temp
            //switch (ddlEmailType.SelectedValue)
            //{
            //    case "UserRegist":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl,EmailTempleteType.UserRegist);
            //        break;
            //    case "ChangePassWord":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.ChangePassWord);
            //        break;
            //    case "FindPassword":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.FindPassword);
            //        break;
            //    case "OrderCreated":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderCreated);
            //        break;
            //    case "OrderPost":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderPost);
            //        break;
            //    case "OrderCancle":
            //        EmUpdate = EmTemplBean.UpdateEmailTemplete(EmTempl, EmailTempleteType.OrderCancle);
            //        break;
            //}
            #endregion
            if (EmUpdate)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('邮件模板更新成功!');", true);
                btnReturn.Visible = true;
                btnUpdate.Visible = false;
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('邮件模板更新失败!');", true);
            }
        }

        /// <summary>
        /// 邮件模板更新页面的返回按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupMenOperatorL.aspx?index=4");
        }
    }
}
